<?php

/**
 * AttractionController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class AttractionController extends Zend_Controller_Action {
	
	private $service;
	
	public function init() {
		$this->service = new PanoramioService();
	}
	
	public function addplacemarkAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
//		$this->_helper->layout()->disableLayout();
        if ($this->_helper->user->isLoggedIn()) {    	
        	$lat = $this->_getParam("lat");
	        $lng = $this->_getParam("lng");
	        $photoTitle = $this->_getParam("name");
	        $mySession = new Zend_Session_Namespace(Constant::SESSION_NAME);
	        
	        $placeMark = new PlaceMarkDTO();
	        $placeMark->latitude = $lat;
	        $placeMark->longitude = $lng;
	        $placeMark->name = $photoTitle;
	        $placeMark->created = $this->_helper->generator->generateCurrentTime();
	        $placeMark->modified = $this->_helper->generator->generateCurrentTime();
	        if (isset($mySession->placeMarks)) {
	            $currentCount = count($mySession->placeMarks);
	            $mySession->placeMarks[$currentCount++] = $placeMark;
	        } else {
	            $mySession->placeMarks = array();
	            $mySession->placeMarks[0] = $placeMark;
	        }
	        
	        $info = $this->view->renderAttractionView($mySession->placeMarks, $mySession->trip, $mySession->step);
	        $mySession->info = $info;
	       
	        $returnJson = array(
            "map" => $mySession->placeMarks,
            "info" => $info
            );
            echo Zend_Json::encode($returnJson);
	    } else {
	       $this->_redirect("/user/login");
	    }
	}
	
	/**
	 * The default action - show the home page
	 */
	public function getpanoramasAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled

		$params = array(
		    "set"=>"public",
		    "from"=>"0",
		    "to"=>"20",
		    "minx"=>$this->_getParam("southWestLng"),
		    "miny"=>$this->_getParam("southWestLat"),
		    "maxx"=>$this->_getParam("northEastLng"),
		    "maxy"=>$this->_getParam("northEastLat"),
		    "size"=>"medium",
		    "mapfilter"=>"true"
		);

		$mySession = new Zend_Session_Namespace(Constant::SESSION_NAME);

		$response = $this->service->call("get_panoramas.php", $params);
		$imgArr = array();
		foreach ($response["photos"] as $key=>$photo) {
		    $img = new PanoramioImg();
		    $img->setPhotoId($photo["photo_id"]);
		    $img->setPhotoTitle($photo["photo_title"]);
		    $img->setUploadDate($photo["upload_date"]);
		    $img->setPhotoFileUrlMedium($photo["photo_file_url"]);
		    $img->setLongitude($photo["longitude"]);
		    $img->setLatitude($photo["latitude"]);
		    $img->setOwnerId($photo["owner_id"]);
		    $img->setOwnerName($photo["owner_name"]);
		    $img->setOwnerUrl($photo["owner_url"]);
		    $img->setWidth($photo["width"]);
		    $img->setHeight($photo["height"]);
		    
		    $imgArr[$key] = $img;
		}
		
		$mySession->panoramio = $imgArr;
		$returnJson = array(
		    "panoramio" => $mySession->panoramio
		);
		echo Zend_Json_Encoder::encode($returnJson);
	}

}

